<template>
  <div class="custom-table-list-wrapper">
    <div class="search" :class="{ active: toggleSearchStatus }">
      <div class="search-left" ref="searchFromRef">
        <el-form class="search-form" inline>
          <el-form-item label="是否启用">
            <el-select
              class="search-select"
              v-model="searchParams.enable"
              placeholder="请选择"
              clearable
            >
              <el-option
                v-for="(item, index) of dictOptions['yn']"
                :key="index"
                :label="item.name"
                :value="Number(item.value)"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="关键词">
            <el-input
              class="search-input"
              v-model="searchParams.name"
              placeholder="请输入园区名称"
              :suffix-icon="Search"
              clearable
            ></el-input>
          </el-form-item>
        </el-form>
        <a
          class="search-left-toggle"
          v-if="showToggle"
          @click="handleToggleSearch"
        >
          <svg-icon :name="toggleSearchStatus ? 'up' : 'down'" />
          {{ toggleSearchStatus ? '收起' : '展开' }}
        </a>
      </div>
      <div class="search-right">
        <el-button type="primary" @click="initTableList(1)"
          ><svg-icon name="search" size="14px" />搜索</el-button
        >
        <el-button @click="handleReset()"
          ><svg-icon name="reset" size="17px" />重置</el-button
        >
      </div>
    </div>
    <div class="content">
      <div class="content-operate">
        <el-button type="primary" @click="handleAdd('新增园区')"
          ><svg-icon name="add" size="15px" />新增园区</el-button
        >
      </div>
      <div class="content-table">
        <el-table class="custom-table" :data="tableData">
          <el-table-column type="index" label="序号" width="60px" />
          <el-table-column prop="name" label="园区名称" />
          <el-table-column prop="address" label="园区地址" />
          <el-table-column prop="area" label="园区面积(m²)" />
          <el-table-column prop="score" label="得分" />
          <el-table-column prop="order" label="土地数">
            <template #default="scope">
              <span>{{ scope.row.mapRegions.length }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="order" label="房屋数">
            <template #default="scope">
              <el-popover placement="bottom" :width="400">
                <template #reference>
                  <span class="link">{{ scope.row.houses.length }}</span>
                </template>
                <div class="popover-detail" v-if="scope.row.houses">
                  <div
                    class="item"
                    v-for="(item, index) of scope.row.houses"
                    :key="index"
                  >
                    {{ item.name }}
                  </div>
                </div>
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column prop="updateTime" label="承租方数">
            <template #default="scope">
              <el-popover placement="bottom" :width="400">
                <template #reference>
                  <span class="link">{{ scope.row.renters.length }}</span>
                </template>
                <div
                  class="popover-detail"
                  v-if="scope.row.renters && scope.row.renters.length > 0"
                >
                  <div
                    class="item"
                    v-for="(item, index) of scope.row.renters || []"
                    :key="index"
                  >
                    {{ item ? item.name : '' }}
                  </div>
                </div>
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column prop="modifyDate" label="更新时间" />
          <el-table-column prop="enable" label="是否启用">
            <template #default="scope">
              <el-switch
                :active-value="'1'"
                :inactive-value="'0'"
                v-model="scope.row.enable"
                :before-change="handleChangeStatus.bind(this, scope.row)"
              />
            </template>
          </el-table-column>
          <el-table-column label="操作" width="120px">
            <template #default="scope">
              <el-button link type="primary" @click="handleDetail(scope.row)"
                >详情</el-button
              >
              <el-button
                link
                type="primary"
                @click="handleEdit(scope.row, '编辑园区')"
                >编辑</el-button
              >
              <!-- <el-button link type="danger" @click="handleDelete(scope.row)"
                >删除</el-button
              > -->
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="content-page">
        <el-pagination
          class="custom-pagination"
          :current-page="paginationParams.pageIndex"
          :page-size="paginationParams.pageSize"
          background
          :page-sizes="[10, 20, 30, 50]"
          layout="total, prev, pager, next, sizes, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="initTableList"
        />
      </div>
    </div>
    <AddDialog ref="addRef" :detail="currentDetail" @close="handleClose" />
    <DetailDialog
      ref="detailRef"
      :detail="currentDetail"
      @close="handleClose"
    />
  </div>
</template>
<script setup>
import { onMounted, ref } from 'vue'
import { useTableList } from '@/hooks/useTableList'
import AddDialog from './add.vue'
import DetailDialog from './detail.vue'
import FileList from '@/components/fileList/index.vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useDict } from '@/hooks/useDict'
const {
  dictOptions,
  initListDictOptions,
  initTreeDictOptions,
  getDictValue,
  getDictStyle,
} = useDict()
import {
  parkPageList,
  parkDelete,
  parkUpdate,
} from '@/api/manager/dataManage/park'
const searchParams = ref({})
const {
  Search,
  toggleSearchStatus,
  showToggle,
  handleToggleSearch,
  tableData,
  paginationParams,
  total,
  initTableList,
  handleSizeChange,
  handleReset,
  handleDelete,
  addRef,
  detailRef,
  currentDetail,
  handleAdd,
  handleEdit,
  handleDetail,
  handleClose,
  searchFromRef,
} = useTableList(searchParams, parkPageList, parkDelete)

const handleChangeStatus = async (val) => {
  return new Promise((resolve) => {
    ElMessageBox.confirm(`是否切换启用状态`, '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    })
      .then(async () => {
        const res = await parkUpdate({
          pkid: val.pkid,
          enable: val.enable === '1' ? '0' : '1',
        })
        if (res.success) {
          ElMessage.success('切换成功')
          resolve(true)
        } else {
          ElMessage.error(res.message)
          resolve(false)
        }
      })
      .catch(() => {
        resolve(false)
      })
  })
}

onMounted(() => {
  initListDictOptions(['yn'])
  initTableList(1)
})
</script>
<style lang="scss" scoped>
@import '@/assets/styles/table.scss';
.popover-detail {
  width: 100%;
  .item {
    width: 100%;
    font-size: 14px;
    color: #757575;
    margin-bottom: 10px;
  }
}
</style>
